﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HsTec.Mobile.Green.SqlServerDAL;
using Baimasoft.FrameWork.Base;
using Baimasoft.Web.UI;
using Baimasoft.Manage.SqlServerDAL;

public partial class BaseData_ProjectForm : Baimasoft.Web.UI.PageFormTop
{
    Project project = new Project();

    /// <summary>
    /// 初始化表单信息
    /// </summary>
    protected override void InitForm()
    {
        HOrgIDCol.Value = AspNetHelper.GetRightOrgIds();   //初始化树的数据权限


        //当前年月级+ 当前0的最大数据;
        txtProjectCode.Text = project.GetMaxProjectCode();


        if (!Page.IsPostBack)
        {
            ddlProjectDataBind();

            try
            {
                string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    string sID = this.Request.QueryString["ID"].ToString().Trim();
                    DataTable MyDT = project.GetProjectInfos(" project_id = '" + sID + "' ");
                    foreach (DataRow dr in MyDT.Rows)
                    {
                        this.txtProjectCode.Text = dr["project_code"].ToString().Trim(); //项目编号
                        this.txtProjectName.Text = dr["project_name"].ToString().Trim(); //项目名称
                        this.txtProjectSname.Text = dr["project_sname"].ToString().Trim(); //项目简称

                        this.DDList_projectlevel.Text = dr["project_level"].ToString().Trim();//项目级别
                        this.DDList_projecttype.Text = dr["project_type"].ToString().Trim();//项目类型

                        this.txtProjectConstructcom.Text = dr["project_constructcom"].ToString().Trim();//建设单位


                        this.ChoiceName_1.Text = dr["projectCustomcomName"].ToString().Trim();//承建单位
                        this.HChoiceID_1.Value = dr["project_customcom"].ToString().Trim();


                        this.ChoiceName_3.Text = dr["projectPrincipalName"].ToString().Trim();//项目负责人
                        this.HChoiceID_3.Value = dr["project_principal"].ToString().Trim();
                        //10

                        this.ChoiceName_2.Text = dr["projectMemberName"].ToString().Trim();//项目组成员
                        this.HChoiceID_2.Value = dr["project_member"].ToString().Trim();

                        this.txtProjectOrgid.Text = dr["project_orgid"].ToString().Trim();//归属组织
                        this.txtProjectStartdate.Text = string.Format("{0:yyyy-MM-dd}", dr["project_startdate"].ToString().Trim());//开始日期

                        this.txtProjectEnddate.Text = string.Format("{0:yyyy-MM-dd}", dr["project_enddate"].ToString().Trim());//结束日期

                        this.txtProjectPid.Text = dr["project_pid"].ToString().Trim();//项目父ID
                        this.txtProjectRemind.Text = dr["project_remind"].ToString().Trim();//预警日期

                        this.DDList_ProjectStatus.SelectedValue = dr["project_status"].ToString().Trim();//项目状态
                        this.txtProjectMemo.Text = dr["project_memo"].ToString().Trim(); //备注 
                    }
                }
                else
                {
                }
            }
            catch
            {
            }
        }
    }

    /// <summary>
    /// 项目下拉框
    /// </summary>
    protected void ddlProjectDataBind()
    {
        this.txtProjectPid.DataSource = project.GetProjectInfos("");
        this.txtProjectPid.DataBind();
        this.txtProjectPid.Items.Insert(0, "无");
        this.txtProjectPid.Items[0].Value = "0";
    }  

    /// <summary>
    /// 保存数据
    /// </summary>
    protected override void SavaData()
    {
        try
        {
            string sid = "";

            if (this.Request.Params["ID"] != null)
            {
                sid = this.Request.Params["ID"].Trim();
            }
            else
            {
                sid = "-1";
            }

            string sProjectCode = this.txtProjectCode.Text;//项目编号
            string sProjectName = this.txtProjectName.Text;//项目名称
            string sProjectSname = this.txtProjectSname.Text;//项目简称

            string sProjectLevel = this.DDList_projectlevel.SelectedValue;
            string sProjectType = this.DDList_projecttype.SelectedValue;

            string sProjectConstructcom = this.txtProjectConstructcom.Text;//建设单位
            string sProjectCustomcom = this.HChoiceID_1.Value;//承建单位
            string sProjectCustomcomName = this.ChoiceName_1.Text.ToString().Trim();
            string sProjectPrincipal = this.HChoiceID_3.Value; // 项目负责人
            string sProjectPrincipalName = this.ChoiceName_3.Text.ToString().Trim();
            //10
            string sProjectMember = this.HChoiceID_2.Value.ToString().Trim(); // 项目组成员
            string sProjectMemberName = this.ChoiceName_2.Text.ToString().Trim();
            int iProjectOrgid = int.Parse(this.txtProjectOrgid.Text);//归属组织
            DateTime DProjectStartdate = Convert.ToDateTime(this.txtProjectStartdate.Text);//开始日期 
            DateTime DProjectEnddate = Convert.ToDateTime(this.txtProjectEnddate.Text);//结束日期

            int iProjectCreator = 0;
            try
            {
                iProjectCreator = int.Parse(Session["Baimasoft_Pers_ID"].ToString());
            }
            catch
            {
                iProjectCreator = 0;
            }
            
            DateTime DProjectCreatedate = DateTime.Now.AddDays(0);

            int iProjectPid = int.Parse(this.txtProjectPid.SelectedValue.ToString().Trim());//项目父ID

            int iProjectSeq = 300;
            string sProjectFlag = "1";//有效标识 0 无效 1 有效
            int iProjectRemind = int.Parse(this.txtProjectRemind.Text);//预警日期
            //20
            string sProjectStatus = this.DDList_ProjectStatus.SelectedValue;
            string sProjectMemo = this.txtProjectMemo.Text;//备注

            string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                int iback = project.InsertProjectInfo(sProjectCode, sProjectName, sProjectSname, 0, sProjectLevel, sProjectType, sProjectConstructcom, sProjectCustomcom, sProjectCustomcomName, sProjectPrincipal, sProjectPrincipalName, sProjectMember, sProjectMemberName, iProjectOrgid, DProjectStartdate, DProjectEnddate, iProjectCreator, DProjectCreatedate, iProjectPid, iProjectSeq, sProjectFlag, iProjectRemind, sProjectStatus, sProjectMemo);
                if (iback > 0)
                {
                    project.UpParentChildNumber(this.txtProjectPid.SelectedValue.ToString().Trim());

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');location.href='ProjectManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                int iback = 0;
                try
                {
                    iback = project.UpdateProjectInfo(sProjectCode, sProjectName, sProjectSname, 0, sProjectLevel, sProjectType, sProjectConstructcom, sProjectCustomcom, sProjectCustomcomName, sProjectPrincipal, sProjectPrincipalName, sProjectMember, sProjectMemberName, iProjectOrgid, DProjectStartdate, DProjectEnddate, iProjectCreator, DProjectCreatedate, iProjectPid, iProjectSeq, sProjectFlag, iProjectRemind, sProjectStatus, sProjectMemo, sid);
                }
                catch
                {
                }

                if (iback > 0)
                {
                    project.UpParentChildNumber(this.txtProjectPid.SelectedValue.ToString().Trim());

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');location.href='ProjectManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Message.AlertMsg(this, "保存失败");
        }
    }

    protected void txtProjectPid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sProjectPID = txtProjectPid.SelectedValue.ToString().Trim();
        if (sProjectPID != "0")
        {
            txtProjectCode.Text = project .GetMaxProjectCode (sProjectPID);
        }
        else
        {
            txtProjectCode.Text = project .GetMaxProjectCode ();
        }
    }
}

